#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 100;
char mp[N][N];
bool vis[N];
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
int n, m, mx, step;

void dfs(int x, int y) {
	if (x<1 || x>n || y<1 || y>m || vis[mp[x][y] - 'A']) return;
	vis[mp[x][y] - 'A'] = true;
	step++;
	mx = max(mx, step);
	for (int i = 0; i < 4; i++) {
		int xx = x + dx[i], yy = y + dy[i];
		dfs(xx, yy);
	}
	vis[mp[x][y] - 'A'] = false;
	step--;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mp[i][j];
		}
	}
	dfs(1, 1);
	cout << mx << endl;
	return 0;
}
